# -*- coding: utf-8 -*-
# $Id: markedmodel.py 19 2009-02-15 00:52:08Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4.QtCore import QAbstractListModel, Qt, QVariant, QString, SIGNAL
from core.batch import Batch
from batchicon import renderBatchIcon

class MarkedModel(QAbstractListModel):
    """Модель для списка помеченных (marked) пачек"""
    
    def __init__(self, parent=None):
        QAbstractListModel.__init__(self, parent)
        self.__marked_batches = []
    
    def data(self, mindex, role):
        if not mindex.isValid():
            return QVariant()
        
        if role == Qt.DisplayRole:
            return QVariant(QString(self.__marked_batches[mindex.row()].name()))
        
        if role == Qt.DecorationRole:
            return QVariant(renderBatchIcon(self.__marked_batches[mindex.row()]))
        
        return QVariant()
    
    def flags(self, mindex):
        if mindex.isValid():
                return Qt.ItemIsEnabled
        else:
            return Qt.NoItemFlags
    
    def rowCount(self, parent_index=None):
        return len(self.__marked_batches)
    
    def onMarkedChanged(self):
        self.__marked_batches = filter(lambda b: b.marked, Batch.all())
        self.emit(SIGNAL("modelReset()"))